package pers.duorhs.easierfeign.iface;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.duorhs.easierfeign.invoker.EasierFeignServiceInvoker;

/**
 * @author Duorhs
 * @date 2022/1/11 15:44
 */
public class GeneralRemoteController {

    private final EasierFeignServiceInvoker invoker;

    public GeneralRemoteController(EasierFeignServiceInvoker invoker) {
        this.invoker = invoker;
    }

    @PostMapping("{service}/{method}")
    @ResponseBody
    public String remoteInvoke(@PathVariable("service") String serviceName, @PathVariable("method") String methodName, @RequestBody String args) {
        String result;
        try {
            result = invoker.getResultAsJsonString(serviceName, methodName, args);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

}
